package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.view.View;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.TaskListItemAdapter;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.mvp.bidding.BiddingTaskListPresenter;
import com.yjhb.android.sbz.mvp.bidding.BiddingTaskListView;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/16 16:20
 */
public class BiddingTaskListActivity extends BaseActivity<BiddingTaskListPresenter> implements OnRefreshLoadmoreListener, BiddingTaskListView, BaseRecyclerAdapter.ItemClickListener<TaskInfo> {
    private final String TAG = "BiddingTaskListActivity";

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.layout_empty)
    View layout_empty;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;

    private final int size = 25;
    private int current = 1;
    List<TaskInfo> tasks = new ArrayList<>();
    TaskListItemAdapter adapter;


    @Override
    protected BiddingTaskListPresenter initPresenter() {
        return new BiddingTaskListPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bidding_task_list;
    }

    @Override
    protected void initUI() {
        recyclerView.setEmptyView(layout_empty);
        recyclerView.setAdapter(adapter = new TaskListItemAdapter(this));
        adapter.setData(tasks);

    }

    @Override
    protected void initData() {
        loadingDialog.show();
        current = 1;
        presenter.getTaskList(size,current);
    }

    @Override
    protected void regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
        adapter.setOnItemClick(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        current++;
        presenter.getTaskList(size, current);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getTaskList(size, current);
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showFAILBottomToast(msg);
        smartRefreshLayout.finishLoadmore();
        smartRefreshLayout.finishRefresh();
    }

    @Override
    public void onLoad(List<TaskInfo> tasks) {
        smartRefreshLayout.finishLoadmore();
        this.tasks.addAll(tasks);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(List<TaskInfo> tasks) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        this.tasks.clear();
        this.tasks.addAll(tasks);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int index, TaskInfo task) {
        Intent intent = new Intent();
        intent.putExtra("selectedTask",task);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void onViewClick(View view, String msg, int index, TaskInfo task) {

    }
}
